/**
 *	\file AKHumanPlayer.m
 *  
 *  \section desc_sec Description
 *  Description
 *  
 *  \if copyright
 *  	Copyright 2008 Ben Chen. All rights reserved.
 *  \endif
 *  
 *  \section hist_sec Change History:
 *  	-	2/3/08		BC		Created this file
 *  
 *  <hr>
 *  \addtogroup AnimalKingdom
 *  @{
 */

#import "AKHumanPlayer.h"
#import "AKStateMaintainer.h"

@interface AKHumanPlayer ()
@property (assign) AKMove		bestMove;
@end

@implementation AKHumanPlayer

@synthesize interactiveDrawing, stateMaintainer, bestMove, side;

// To avoid warning
- (id)initWithSide:(AKSideIndex)aSide 
	  numOfPlayers:(unsigned)playersCount {
	self = [super init];
	if (self != nil) {
		self.side = aSide;
	}
	return self;
}

#pragma mark Mouse Events

- (void)mouseMoved:(NSEvent *)theEvent {	
	NSPoint pointInView = [(interactiveDrawing.drawingView) convertPoint:[theEvent locationInWindow]
																fromView:nil];
	
	AKCellIndex cellIndex = [interactiveDrawing cellIndexForPoint:pointInView];
	if (cellIndex != NSNotFound) {
		AKPiece aPiece = [stateMaintainer moveablePieceAtCell:cellIndex];
				
		if (!AKEqualPieces(aPiece, AKZeroPiece) && aPiece.side == self.side)
			[interactiveDrawing activatePiece:aPiece];
		else
			[interactiveDrawing deactivatePiece];
	}
}

- (void)dragPiece:(AKPiece)draggedPiece {
	while (1) {
		NSEvent *theEvent = [[(interactiveDrawing.drawingView) window] nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
		NSPoint pointInView = [(interactiveDrawing.drawingView) convertPoint:[theEvent locationInWindow]
																	fromView:nil]; 
		[interactiveDrawing dragPieceAt:pointInView];
		
		if ([theEvent type] == NSLeftMouseUp) {			
			AKCellIndex dstCell = [interactiveDrawing cellIndexForPoint:pointInView];
			if (dstCell != NSNotFound) {
				NSArray *dstArray = [stateMaintainer reachableCellsForPiece:draggedPiece];
				if ([dstArray containsObject:[NSNumber numberWithUnsignedInt:dstCell]]) {
					[interactiveDrawing stopDraggingShouldComeBack:NO];

					AKMove newMove;
					newMove.piece = draggedPiece.piece;
					newMove.srcCell	= draggedPiece.cell;
					newMove.dstCell	= dstCell;
					newMove.side	= draggedPiece.side;
					
					[interactiveDrawing.drawingView setNextResponder:nil];
					self.bestMove = newMove;
				} else {
					[interactiveDrawing stopDraggingShouldComeBack:YES];
				}
			} else {
				[interactiveDrawing stopDraggingShouldComeBack:YES];
			}
			
			break;
		}
	}
}

- (void)mouseDown:(NSEvent *)theEvent {		
	NSPoint pointInView = [(interactiveDrawing.drawingView) convertPoint:[theEvent locationInWindow]
																fromView:nil]; 
	AKCellIndex cellIndex = [interactiveDrawing cellIndexForPoint:pointInView];
	
	AKPiece moveablePiece = [stateMaintainer moveablePieceAtCell:cellIndex];
	
	if (AKEqualPieces(moveablePiece, AKZeroPiece)) {		
		NSArray *newPieces = [stateMaintainer newPiecesAtCell:cellIndex];
		
		if (newPieces) {
			AKMove newMove;
			newMove.piece	= [[newPieces objectAtIndex:0] unsignedIntValue];
			newMove.side	= self.side;
			newMove.srcCell	= NSNotFound;
			newMove.dstCell	= cellIndex;
			[interactiveDrawing.drawingView setNextResponder:nil];
			self.bestMove = newMove; // should trigger observer's method
		}
	} else {
		[interactiveDrawing activatePiece:moveablePiece];	// make sure it's activated before dragging
		[interactiveDrawing aboutToDragPiece:moveablePiece atPoint:pointInView];
		[self dragPiece:moveablePiece];
	}
}

//- (void) mouseUp:(NSEvent *)theEvent {
//}

#pragma mark AKPlayer methods

- (void)backToInitialState {
}

- (void)thinkNextMove {
	[(interactiveDrawing.drawingView) setNextResponder:self];
}

- (BOOL)performMove:(AKMove)newMove {
	return YES;	// Since humen should "maintain" the state themselves
}

#pragma mark Plug-in

- (NSString*)gameName {
	return [NSString string];
}

@end

/** @} */
